﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.utils.*;
	public class Tip extends Sprite {
		private var tt:TextField;
		private var tf:TextFormat;
		private var tl:Array;
		//延时
		private var delay:Number;
		//保持
		private var hold:Number;
        private var intervalId:uint;
		public function Tip(_delay:Number = 100, _hold:Number = 0) {
			delay = _delay;
			hold = _hold;
			//
			tf = gF();
			tt = gT();
			addChild(tt);
			hide();
			addEventListener(Event.ADDED_TO_STAGE, addHd);
		}
		private function addHd(e:Event):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHd);
		}
		private function moveHd(e:MouseEvent):void {
			move();
		}
		//字符显示
		public function show(str:String):void {
			clearTimeout(intervalId);
			if (str) {
				intervalId = setTimeout(sM, delay, str);
			}
		}
		//移动字符
		private function move():void {
			//取得位置
			var tx:Number = stage.mouseX - tt.width * 0.5;
			var ty:Number = stage.mouseY - tt.height - 5;
			if (ty < 0) {
				ty = stage.mouseY + 10;
			}
			//修正位置
			fP(tx, ty);
		}
		private function sM(str:String):void {
			tt.text = str;
			tt.setTextFormat(tf);
			//
			move();
			//立即显示
			sN();
		}
		//修正位置
		private function fP(tx:Number, ty:Number):void {
			if (tx < 0) {
				tx = 0;
			} else if (tx > stage.stageWidth - tt.width) {
				tx = stage.stageWidth - tt.width;
			}
			tt.x = tx;
			tt.y = ty;
		}
		//立即显示
		private function sN():void {
			//显示
			tt.visible = true;
			//超时隐藏
			if (hold) {
				intervalId = setTimeout(hide, hold);
			}
		}
		//隐藏----------------------------------------------------------------
		public function hide():void {
			clearTimeout(intervalId);
			tt.visible = false;
		}
		//格式
		private function gF():TextFormat {
			var tf:TextFormat = new TextFormat();
			tf.size = 12;
			tf.font = "Verdana";
			tf.leftMargin = 3;
			tf.rightMargin = 2;
			return tf;
		}
		//提示
		private function gT():TextField {
			var tip:TextField = new TextField();
			tip.selectable = false;
			tip.mouseEnabled = false;
			tip.autoSize = "left";
			tip.background = true;
			tip.backgroundColor = 0xffffe1;
			tip.filters = gS(0x000000, 0.2, 4);
			return tip;
		}
		//滤镜
		public function gS(c:Number,a:Number,distance:Number = 1.0, angle:Number = 45, blurX:Number = 4.0, blurY:Number = 4.0, strength:Number = 1.0, quality:int = 1, inner:Boolean = false, knockout:Boolean = false, hideObject:Boolean = false):Array {
			//distance:Number = 4.0, angle:Number = 45, color:uint = 0, alpha:Number = 1.0, blurX:Number = 4.0, blurY:Number = 4.0, 
			//strength:Number = 1.0, quality:int = 1, inner:Boolean = false, knockout:Boolean = false, hideObject:Boolean = false
			return new Array(new DropShadowFilter(distance,angle,c,a,blurX,blurY,strength,quality,inner,knockout,hideObject));
		}
	}
}